// pages/releaseList/releaseList.js
import {
  getFirstAddress,
  getSecondAddress,
}
from "../../apis/address_api"
import {
  getUrgeAdvise,
  urgeAdvise
}
from "../../apis/advise_api"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 一级辖区
    firstAddress: "",
    // 二级辖区
    secondAddress: "",
    // 一级管理员辖区列表
    locationList: [
      [],
      []
    ],
    //建议列表
    adviseList: [],
    //建议列表数量
    total:0,
    isShow: false,
    // 时间类型类表（-1：全部,0:一天内,1:三天内,2:七天内,3:一月内,4:半年内）
    timeList: [{
      timeType: -1,
      time: "全部"
    }, {
      timeType: 0,
      time: "一天以上"
    }, {
      timeType: 1,
      time: "三天以上"
    }, {
      timeType: 2,
      time: "七天以上"
    }, {
      timeType: 3,
      time: "一月以上"
    }, {
      timeType: 4,
      time: "半年以上"
      }],
    // 催办次数（-1：全部,0:零次,1:一到三次,2:四到五次,3:五次以上）
    countList: [{
      count: -1,
      value:"全部"
    },{
      count: 0,
      value:"零次"
    },{
      count: 1,
      value:"一到三次"
    },{
      count: 2,
      value:"四到五次"
    },{
      count: 3,
      value:"五次以上"
    }],
    // 时间条件
    time: '全部',
    // 催办次数
    countValue:"全部",
    // 时间类型（-1：全部,0:一天内,1:三天内,2:七天内,3:一月内,4:半年内）
    timeType: -1,
    // 地点关键字
    address: "",
    // 内容关键字
    key: "",
    // 催办次数
    count:-1
  },

  //空方法，去除警告，不可删除
  onInput() {
    // 空方法，去除警告，不可删除
  },

  // 呈现弹窗
  show() {
    this.setData({
      isShow: true
    })
  },

  // 退出弹窗
  quit() {
    this.setData({
      isShow: false
    })
  },

  // 获取时间类型
  getTimeType(e) {
    this.setData({
      time: this.data.timeList[e.detail.value].time,
      timeType: this.data.timeList[e.detail.value].timeType,
    })
  },
  // 获取催办次数类型
  getCount(e) {
    this.setData({
      count: this.data.countList[e.detail.value].count,
      countValue: this.data.countList[e.detail.value].value,
    })
  },

  // 重置筛选数据
  reset() {
    this.setData({
      time: '全部',
      timeType: -1,
      address: "",
      key: ""
    })
  },
  // 一键催办
  urgeAdvise () {
    let adviseList = this.data.adviseList
    wx.showModal({
      title: '提示',
      content: '共' + adviseList.length +"条数据，确认催办？",
      success: res => {
        if (res.confirm) {
          urgeAdvise(adviseList).then(res => {
            if (res.code == 200) {
              this.getUrgeAdvise()
              wx.showToast({
                title: res.message,
                icon: 'success',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: res.message,
                icon: 'error',
                duration: 2000
              })
            }
            })
        }
      }
    })
    
  },


  // 条件查询数据
  check() {
    this.setData({
      adviseList: [],
    })
    this.getUrgeAdvise()
    this.quit()
  },

  // 跳转页面
  jumpPage(e) {
    let id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/pages/handle/handle?id=' + id,
      })
  },

  // 条件查找所有建议
  getUrgeAdvise () {
    let firstAddress = this.data.firstAddress == "全部" ? "" : this.data.firstAddress
      getUrgeAdvise(firstAddress, this.data.secondAddress, this.data.count, this.data.timeType, this.data.address, this.data.key).then(res => {
        let adviseList = res.data
        this.setData({
          adviseList: adviseList
        })
      })
  },
  //获取选择的管理员辖区
  updateAdminAddress(e) {
    this.setData({
      secondAddress: this.data.addressList[e.detail.value]
    })
    this.reset()
    this.check()
  },

  //获取选择的辖区
  getAddress(e) {
    let locationList = this.data.locationList
    let secondAddress = locationList[1][e.detail.value[1]].address
    this.setData({
      firstAddress: locationList[0][e.detail.value[0]].address,
      secondAddress: secondAddress == "全部" ? "" : secondAddress
    })
    this.reset()
    this.check()
  },

  //更新二级辖区列表
  updateSecondAddress(e) {
    if (e.detail.column == 0) {
      let id = this.data.locationList[0][e.detail.value].id
      this.getSecondAddress(id)
    }
  },

  //获取一级辖区列表
  getFirstAddress() {
    getFirstAddress().then(res => {
      res.data.unshift({
        id: 0,
        address: "全部"
      })
      
      this.setData({
        'locationList[0]': res.data,
        firstAddress: res.data[0].address
      })
    })
  },

  //获取二级辖区列表
  getSecondAddress(id) {
    getSecondAddress(id).then(res => {
      res.data.unshift({
        id: 0,
        address: "全部"
      })
      
      this.setData({
        'locationList[1]': res.data,
      });
    })
  },

  // 初始化页面数据
  getPageData() {
    wx.setNavigationBarTitle({
      title: '上报信息',
    })
      this.getFirstAddress()
      this.getSecondAddress(0)
      this.getUrgeAdvise()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPageData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onshow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.reset()
    this.setData({
      adviseList: [{
          total: 0,
          records: []
        },
        {
          total: 0,
          records: []
        },
        {
          total: 0,
          records: []
        },
        {
          total: 0,
          records: []
        }
      ]
    })
    this.getPageData()
    return;
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
})